package com.hhl.flower.controller;

import com.hhl.flower.common.result.Result;
import com.hhl.flower.pojo.vo.OrderSubmitVO;
import com.hhl.flower.pojo.vo.OrderVO;
import com.hhl.flower.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user/order")
@Api(tags = "订单相关接口")
@Slf4j
public class OrderController {

	@Autowired
	private OrdersService ordersService;

	@GetMapping("/list")
	@ApiOperation("获取订单列表")
	public Result<List<OrderVO>> listResult(@RequestParam("status") Integer status) {
		List<OrderVO> orderVOList = ordersService.listByStatus(status);
		return Result.success(orderVOList);
	}

	@PostMapping("/submit")
	@ApiOperation("用户下单")
	public Result<OrderSubmitVO> submit(@RequestParam("sids") String sids, @RequestParam("aid") Integer aid, @RequestParam(value = "cid", required = false) Integer cid, @RequestParam("amount") Double amount,  @RequestParam("remark") String remark) {

		// 将 sids 字符串转换为 List<Integer>
		List<Integer> sidsList = Arrays.stream(sids.split(","))
				.map(Integer::parseInt)
				.collect(Collectors.toList());
		OrderSubmitVO orderSubmitVO = ordersService.submitOrder(sidsList, aid, cid, amount, remark);
		return Result.success(orderSubmitVO);
	}

	@PostMapping("/payment")
	@ApiOperation("订单支付")
	public Result payment(@RequestParam("number") String number, @RequestParam("payMethod") Integer payMethod) {

		ordersService.payOrder(number, payMethod);
		return Result.success();
	}

	@PostMapping("/cancel")
	@ApiOperation("取消订单")
	public Result cancel(@RequestParam("number") String number) {

		ordersService.cancelOrder(number);
		return Result.success();
	}

	@PostMapping("/confirm")
	@ApiOperation("确认收货")
	public Result confirm(@RequestParam("number") String number) {

		ordersService.confirmOrde(number);
		return Result.success();
	}




}
